﻿using AutoMapper;
using B.S.BasicData.Api.Read.Application.Command.ShenPi;
using B.S.BasicData.Domian.Entities.Account;
using B.S.BasicData.Domian.Entities.shenpi;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;
using System.Security.Principal;

namespace B.S.BasicData.Api.Read.Application.Handler.ShenPi
{
    public class ShenPiDelHandler : IRequestHandler<ShenpiDelCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<LeaveModel> laeveRepo;
        private readonly IMapper mapper;

        public ShenPiDelHandler(IBaseRepository<LeaveModel>laeveRepo,IMapper mapper)
        {
            this.laeveRepo = laeveRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(ShenpiDelCommand request, CancellationToken cancellationToken)
        {
            var res=new ApiResult<int>();
            var leave=laeveRepo.GetAll().Where(x=>x.Id==request.Id);
            if (leave == null)
            {
                res.Message = "姓名不存在";
                res.Code = ApiEnum.Error;
                return Task.FromResult(res);
            }
            var entity = mapper.Map<LeaveModel>(request);
            entity.IsDel = true;
            var r = laeveRepo.Update(entity).Result;
            if (r > 0)
            {
                res.Message = "删除成功";
                res.Code = ApiEnum.Ok;
                res.Data = r;
            }
            else
            {
                res.Message = "删除失败";
                res.Code = ApiEnum.Error;
                res.Data = r;
            }
            return Task.FromResult(res);
        }
    }
}
